﻿document.write("<script language='javascript' src='/resources/js/views/defineTable.js'></script>");
document.write("<script language='javascript' src='/resources/js/views/defineInit.js'></script>");
var action = "/area/list.do";

var page = 1;
var pageSize = 30;

function query(curPage, pageSize) {

	curPage = parseInt(curPage);
	pageSize = parseInt(pageSize);
	var searchname = $('#searchname').val();
	var pid = null;
	var level = null;
	var status = 1;

	$.get(action, {
		"name" : searchname,
		"pid" : pid,
		"level" : level,
		"status" : status,
		"page" : curPage,
		"pageSize" : pageSize
	}, function(data) {

		//var areaAll = data["areaAll"];
		var total = data.data.size;
		var totalPage = Math.ceil(total / pageSize);
		var start = (curPage - 1) * pageSize;

		data = data.data["results"];

		/*for (var i = 0; i < data.length; i++) {
			// 获取父级地域名称
			if (data[i]["pid"] == 0) {
				data[i]["pname"] = "";
			} else {
				for (var j = 0; j < areaAll.length; j++) {
					if (data[i]["pid"] === areaAll[j]["id"]) {
						data[i]["pname"] = areaAll[j]["name"];
						break;
					}
				}
			}
		}*/
		
		var iHeight = document.documentElement.clientHeight;
		$('#tt').datagrid({
			width : '100%',
			height : iHeight-110,
			toolbar : defineInitCreateToolBar(true, true, true,false),
			fitColumns : true,
			columns : [ defineTableAreaShowColumns ],
			singleSelect : true,// 是否单选
			data : data,
			pageNumber : curPage,// 显示数据列用
			pageSize : pageSize,
			pageList : [ 30, 50, 100 ],
			rownumbers : false,
			pagination : true
			
		});

		var p = $('#tt').datagrid('getPager');
		$(p).pagination(
				{
					total : total,
					pageList : [ 30, 50, 100 ],
					pageSize : pageSize,
					pageNumber : curPage,
					beforePageText : '第',
					afterPageText : '页    共 ' + totalPage + ' 页',
					displayMsg : '当前显示 ' + (start + 1) + '-'
							+ (start + data.length) + ' 条记录 共 ' + total
							+ ' 条记录',
					onSelectPage : function(curPage, pageSize) {
						query(curPage, pageSize);
					},
					onChangePageSize : function(ps) {
						pageSize = ps;
					}
				});
	}, "json");
}

$(document).ready(function() {

	$.ajaxSetup({
		cache : false
	});

	query(page, pageSize);

	$('#fmAdd').form({
		success : function(data) {
			CRUD.queryList();
			try {
				var result = eval('(' + data + ')');
				if (result.success == true) {
					$('#dialogAdd').dialog('close');
				} else {
					$.messager.alert('提示', '保存失败');
				}
			} catch (err) {
			}
		}
	});

	CRUD.queryList();
	InitForm();

	// 确定添加
	$('#a_AddOK').click(function() {

		if (!document.getElementById("name").value) {
			$.messager.alert('提示', '部门名称不能为空!');
			return;
		}
		if (!$('#pid').combobox("getValue")) {
			$.messager.alert('提示', '上级部门不能为空!');
			return;
		}
		if (!$('#level').combobox("getValue")) {
			$.messager.alert('提示', '部门级别不能为空!');
			return;
		}

		$('#fmAdd').submit();
		CRUD.queryList();
	});

	// 取消
	$('#a_AddCancle').click(function() {
		var textarea = $('#idScales');
		if (textarea) {
			textarea.remove();
		}
		$('#fmAdd').form('clear');
		$('#dialogAdd').dialog('close');
		$('#tt').datagrid("clearSelections");
	});
});

var CRUD = {
	queryList : function() {
			query(page, pageSize);
	},

	Add : function() {
		$('#fmAdd').form('clear');
		var textarea = $('#idScales');
		if (textarea) {
			textarea.remove();
		}
		$('#dialogAdd').dialog('open');
		$('#tt').datagrid("clearSelections");
		$('#dialogAdd').dialog("setTitle", "添加指标分类");
	},

	Del : function() {
		var r = $('#tt').datagrid('getSelected');
		if (r) {
			$.messager.confirm('删除提示', '你确认删除这一条数据吗？', function(d) {
				if (d) {
					$.get(
							getContextPath() + "/area/delete.do?id="
									+ r.id, function(data) {
								if (data == 'true') {
									$.messager.show({
										title : '系统提示',
										msg : '删除成功！',
										timeout : 1500
									});
									$('#tt').datagrid("clearSelections");
									CRUD.queryList();
								} else {
									$.messager.alert('错误', '删除失败！');
								}
							});
				}
			});
		} else {
			$.messager.alert('提示', '请选择一行数据!');
		}
	},
	Update : function() {
		var r = $('#tt').datagrid('getSelected');
		if (r == null) {
			$.messager.alert('提示', '请选择一行数据');
			return;
		}
		$('#dialogAdd').dialog('open');
		$('#fmAdd').form('load', r);
		$('#dialogAdd').dialog("setTitle", "修改部门");
	}
};


function modify(id, code, name, pname, level, pid) {

	code = setEmptyForm(code);
	name = setEmptyForm(name);
	pname = setEmptyForm(pname);
	level = setEmptyForm(level);
	pid = setEmptyForm(pid);

	$('#dialogAdd').dialog('open');
	$('#fmAdd').form('load', {
		id : id,
		code : code,
		name : name,
		pname : pname,
		level : level,
		pid : pid
	});
	$('#dialogAdd').dialog("setTitle", "修改指标分类");
}

function del(id) {
	$.messager.confirm('删除该地域', '你确认删除该地域吗?', function(d) {
		if (d) {
			$.get(getContextPath() + "/area/delete.do?id=" + id,
					function(data) {
						if (data == 'true') {
							$.messager.show({
								title : '系统提示',
								msg : '删除成功！',
								timeout : 1500
							});
							$('#tt').datagrid("clearSelections");
							CRUD.queryList();
						} else {
							$.messager.alert('错误', '删除失败！');
						}
					});
		}
	});
}

function InitForm() {
	var width = 500;
	var height = window.screen.height;
	if (height > 200) {
		height = 200;
	}
	$('#dialogAdd').dialog({
		closed : true,
		title : "添加指标分类",
		width : width,
		height : height
	});
}

function setEmptyForm(value) {
	if (typeof (value) == "undefined" || value == "null") {
		return "";
	} else {
		return value;
	}
}

function up(id) {
	$.get(getContextPath() + "/area/sort.do?id=" + id+"&aim=1", function (data) {
		if (data == 'true') {
			$.messager.show({
				title: '系统提示',
				msg: '排序成功！',
				timeout: 1500
			});
			$('#tt').datagrid("clearSelections");
			CRUD.queryList();
		} else {
			$.messager.alert('错误', '删除失败！');
		}
	});
}

function down(id) {
	$.get(getContextPath() + "/area/sort.do?id=" + id+"&aim=2", function (data) {
		if (data == 'true') {
			$.messager.show({
				title: '系统提示',
				msg: '排序成功！',
				timeout: 1500
			});
			$('#tt').datagrid("clearSelections");
			CRUD.queryList();
		} else {
			$.messager.alert('错误', '删除失败！');
		}
	});
}

window.onresize = function () {
	var iHeight = document.documentElement.clientHeight;
	$("#tt").datagrid('resize',{height:iHeight-110+'px'});
}